﻿using HK.Core.Serializable.Attribute;
using HK.Editor.Serializable.Protocol.Drawer;
using UnityEditor;
using UnityEngine;

namespace HK.Editor.Serializable.Drawer
{
    /// <summary>
    /// Drawer - 序列化文件Body
    /// </summary>
    [CustomPropertyDrawer(typeof(SBodyAttribute))]
    public class SBodyDrawer : GUIPropertyDrawer<SBodyAttribute>, ISBodyDrawer
    {

#region SerializedProperty

        /// <summary>
        /// 初始化序列化Property信息
        /// </summary>
        /// <param name="iSelf">自身Property</param>
        /// <returns>true:OK; false:NG;</returns>
        protected override bool InitSerializedProperties(SerializedProperty iSelf)
        {
            if (!base.InitSerializedProperties(iSelf)) return false;
            
            // 只读
            if (ReadOnly)
            {
                SelfProperty.isExpanded = false;
            }
            
            return true;
        }

#endregion

#region Size&Rect

        /// <summary>
        /// 取得Property的高度
        /// </summary>
        /// <param name="iProperty">Property</param>
        /// <param name="iLabel">文本</param>
        /// <returns>Property的高度</returns>
        public override float GetPropertyHeight(SerializedProperty iProperty, GUIContent iLabel)
        {
            var totalHeight = base.GetPropertyHeight(iProperty, iLabel);
            if (iProperty.isExpanded)
            {
                totalHeight += 6;
            }
            
            TotalHeight = totalHeight;
            return TotalHeight;
        }

#endregion
        
#region Events

        /// <summary>
        /// 展开
        /// </summary>
        /// <param name="iSelfProperty">自身Property</param>
        /// <param name="iIsExpanded">展开/缩进</param>
        protected override void ExpandChildren(SerializedProperty iSelfProperty, bool iIsExpanded)
        {
            if(null == iSelfProperty) return;
            iSelfProperty.isExpanded = iIsExpanded;
                    
            base.ExpandChildren(iSelfProperty, iIsExpanded);
        }

#endregion
        
    }
}
